package com.wenhao.leetcode.medium;

import com.sun.management.GarbageCollectionNotificationInfo;

/**
 * 题目：https://leetcode-cn.com/problems/number-of-islands/
 * 岛屿数量
 *
 * 典型的图的遍历的问题
 * 既可以用dfs也可以用bfs
 *
 * @author Wenhao Tong
 * @create 2021-07-17 19:17
 */
public class LeetCode200 {
    public static void main(String[] args) {

    }

    public int numIslands(char[][] grid) {
        for (int i = 0;i < grid.length;i++) {
            for (int j = 0;j < grid[i].length;j++) {
                if (grid[i][j] == '1') {
                    count++;
                    dfs(i,j,grid);
                }
            }
        }
        return count;
    }
    int count = 0;
    public void dfs (int x,int y,char[][] grid) {
        if (grid[x][y] == '0') {
            return;
        }
        grid[x][y] = '0';
        if (x < grid.length - 1) {
            dfs(x + 1,y,grid);
        }
        if (x > 0) {
            dfs(x - 1,y,grid);
        }
        if (y < grid[0].length - 1) {
            dfs(x,y + 1,grid);
        }
        if (y > 0) {
            dfs(x,y - 1,grid);
        }
    }
}
